package com.cnfilm.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.cnfilm.utils.CacheManager;
import com.cnfilm.utils.DateUtils;
import com.cnfilm.vo.FilmAllVO;
import com.cnfilm.vo.FilmVO;
import com.cnfilm.vo.SeeAllVO;
import com.cnfilm.web.category.Category;
import com.cnfilm.web.code.Code;
import com.cnfilm.web.film.Film;
import com.cnfilm.web.links.Links;
import com.cnfilm.web.website.Website;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

/**
 * CommonController
 */
public class CommonController extends Controller {
	
	/**
	 * 网站首页
	 */
	public void index() {
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		/**搜索下面的文章**/
		List<Film> searchList = getSearchList(menuList);
		setAttr("searchList", searchList);
		/**点击排行**/
		List<Film> djphList = getDjphList();
		setAttr("djphList", djphList);
		/**最近更新**/
		List<Film> zjgxList = getZjgxList();
		setAttr("zjgxList", zjgxList);
		/**首页下的总分类详细**/
		List<FilmAllVO> filmAllList = getSeeAllList(menuList);
		setAttr("filmList", filmAllList);
		/**获取友情链接**/
		List<Links> linkList = Links.getAllLinks();
		setAttr("linkList", linkList);
		/**查询观影排行**/
		List<SeeAllVO> seeAllList = getSeefvolist(menuList);
		setAttr("gkphList", seeAllList);
		render("/view/index.jsp");
	}
	
	/**
	 * 最近更新
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getZjgxList(){
		List<Film> zjgxList = new ArrayList<Film>();
		String key = "cache_getZjgxList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			zjgxList = (List<Film>)CacheManager.getCache(key);
		}else{
			zjgxList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 10");
			CacheManager.putCache(key, zjgxList);
		}
		return zjgxList;
	}
	
	/**
	 * 点击排行
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getDjphList(){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<Film> djphList = new ArrayList<Film>();
		String key = "cache_getDjphList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			djphList = (List<Film>)CacheManager.getCache(key);
		}else{
			djphList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.addtime>=? order by A.clicknum desc limit 25",lasttime);
			CacheManager.putCache(key, djphList);
		}
		return djphList;
	}
	
	/**
	 * 搜索下面的文章
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getSearchList(List<Category> menuList){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<Film> searchList = new ArrayList<Film>();
		String key = "cache_getSearchList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			searchList = (List<Film>)CacheManager.getCache(key);
		}else{
			Film djpht = null;
			int num = 1;
			for(Category c:menuList){
				if(num<=8){
					djpht = Film.dao.findFirst("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? and A.addtime>=? order by A.clicknum desc limit 1",c.getInt("cid"),lasttime);
					if(djpht==null){
						continue;
					}
					searchList.add(djpht);
				}else{
					break;
				}
				num++;
			}
			CacheManager.putCache(key, searchList);
		}
		return searchList;
	}
	
	/**
	 * 首页下的总分类详细
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<FilmAllVO> getSeeAllList(List<Category> menuList){
		List<FilmAllVO> filmAllList = new ArrayList<FilmAllVO>();
		String key = "cache_getSeeAllList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			filmAllList = (List<FilmAllVO>)CacheManager.getCache(key);
		}else{
			FilmAllVO vo = null;
			List<Film> flist = null;
			List<FilmVO> fvolist = null;
			FilmVO fvo = null;
			for(Category c:menuList){
				fvolist = new ArrayList<FilmVO>();
				vo = new FilmAllVO();
				vo.setCname(c.getStr("cname"));
				vo.setCurl(c.getStr("url"));
				flist = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? order by A.addtime desc limit 25",c.get("cid"));
				long fnum = Db.queryLong("select count(1) from film where cid = ? ",c.get("cid"));
				vo.setFnum(fnum);
				for(Film f:flist){
					fvo = new FilmVO();
					fvo.setCurl(f.getStr("curl"));
					fvo.setUrl(f.getStr("url"));
					fvo.setFname(f.getStr("fname"));
					fvolist.add(fvo);
				}
				vo.setList(fvolist);
				filmAllList.add(vo);
			}
			CacheManager.putCache(key, filmAllList);
		}
		return filmAllList;
	}
	
	/**
	 * 查询观影排行
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<SeeAllVO> getSeefvolist(List<Category> menuList){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<SeeAllVO> seeAllList = new ArrayList<SeeAllVO>();
		String key = "cache_getSeefvolist_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			seeAllList = (List<SeeAllVO>)CacheManager.getCache(key);
		}else{
			SeeAllVO seevo = null;
			List<FilmVO> seefvolist = null;
			FilmVO seefvo = null;
			for(Category c:menuList){
				seefvolist = new ArrayList<FilmVO>();
				seevo = new SeeAllVO();
				seevo.setCname(c.getStr("cname"));
				seevo.setCurl(c.getStr("url"));
				String sql = "select A.url,A.fname,A.addtime,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? and A.addtime>=? and A.fid in(select X.fid from (select sum(clicknum) as num,fid from video group by fid) X order by X.num desc) order by A.clicknum,A.addtime desc limit 10";
				List<Film> gkphList = Film.dao.find(sql,c.get("cid"),lasttime);
				for(Film f:gkphList){
					seefvo = new FilmVO();
					seefvo.setCurl(f.getStr("curl"));
					seefvo.setUrl(f.getStr("url"));
					seefvo.setFname(f.getStr("fname"));
					seefvo.setTime(f.getStr("addtime").substring(5, 10));
					seefvolist.add(seefvo);
				}
				seevo.setList(seefvolist);
				seeAllList.add(seevo);
			}
			CacheManager.putCache(key, seeAllList);
		}
		return seeAllList;
	}
}
